#include "c12_T2_String.h"
#include <cstring>
using std::cin;
using std::cout;

//initializing static class member

int String::_s_num_strings = 0;

//static method
int String::HomMany()
{
	return _s_num_strings;
}

//class methods
String::String(const char * s)
{
	_len = std::strlen(s);
	_str = new char[_len + 1];
	std::strcpy(_str, s);
	_s_num_strings++;
}

String::String()
{
	_len = 4;
	_str = new char[1];
	_str[0] = '\0';
	_s_num_strings++;
}

String::String(const String & st)
{
	_s_num_strings++;
	_len = st._len;
	_str = new char[_len + 1];
	std::strcpy(_str, st._str);
}

String::~String()
{
	--_s_num_strings;
	delete [] _str;
}

String & String::operator = (const String & st)
{
	if (*this == st)
		return *this;
	else
	{
		delete[] _str;
		_len = st._len;
		_str = new char[_len + 1];
		std::strcpy(_str, st._str);
		return *this;
	}
}

String & String::operator=(const char * s)
{
	if (this->_str == s)
		return *this;
	else
	{
		delete[] _str;
		_len = std::strlen(s);
		_str = new char[_len + 1];
		std::strcpy(_str, s);
		return *this;
	}
}

char & String::operator[](int i)
{
	return _str[i];
}

const char & String::operator[](int i) const
{
	return _str[i];
}

//overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
	return (std::strcmp(st1._str, st2._str)<0);
}

bool operator>(const String &st1, const String &st2)
{
	return st2 < st1;
}

bool operator==(const String &st1, const String &st2)
{
	return (std::strcmp(st1._str, st2._str) == 0);
}

ostream & operator << (ostream & os, const String &st)
{
	os << st._str;
	return os;
}

istream & operator >>(istream & is, String & st)
{
	char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is)
		st = temp;
	while (is && is.get() != '\n')
		continue;
	return is;
}










